/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.OperationWebServiceUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.GrantCard;
import com.thinkgem.jeesite.modules.guobo.service.GrantCardService;

/**
 * 授权卡Controller
 * @author wudavid
 * @version 2017-05-02
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/grantCard")
public class GrantCardController extends BaseController {

	@Autowired
	private GrantCardService grantCardService;
	
	@ModelAttribute
	public GrantCard get(@RequestParam(required=false) String id) {
		GrantCard entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = grantCardService.get(id);
		}
		if (entity == null){
			entity = new GrantCard();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:grantCard:view")
	@RequestMapping(value = {"list", ""})
	public String list(GrantCard grantCard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GrantCard> page = grantCardService.findPage(new Page<GrantCard>(request, response), grantCard); 
		model.addAttribute("page", page);
		return "modules/guobo/grantCardList";
	}

	@RequiresPermissions("guobo:grantCard:view")
	@RequestMapping(value = "form")
	public String form(GrantCard grantCard, Model model) {
		model.addAttribute("grantCard", grantCard);
		return "modules/guobo/grantCardForm";
	}

	@RequiresPermissions("guobo:grantCard:edit")
	@RequestMapping(value = "save")
	public String save(GrantCard grantCard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, grantCard)){
			return form(grantCard, model);
		}
		grantCardService.save(grantCard);
		addMessage(redirectAttributes, "保存授权卡成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/grantCard/?repage";
	}
	
	@RequiresPermissions("guobo:grantCard:edit")
	@RequestMapping(value = "delete")
	public String delete(GrantCard grantCard, RedirectAttributes redirectAttributes) {
		grantCardService.delete(grantCard);
		addMessage(redirectAttributes, "删除授权卡成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/grantCard/?repage";
	}

	
	/**
	 * 授权
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("guobo:grantCard:edit")
	@ResponseBody
	@RequestMapping(value = "grant")
	 public Map<String,Object> grant(HttpServletRequest request,HttpServletResponse response, Model model) {
		Map<String,Object> data = new HashMap<String,Object>();
		String id = request.getParameter("id");
		data.put("code", "1");
		data.put("msg", "授权失败");
		try{
			GrantCard grantCard = grantCardService.get(id);
			if(grantCard!=null){
				List<String> rfids = new ArrayList<String>();
				rfids.add(grantCard.getRfid());
				String result = OperationWebServiceUtils.grant("Add",rfids);
				if("0".equals(result)){
					grantCard.setStatus("1");
					grantCardService.save(grantCard);
					data.put("code", "0");
					data.put("msg", "授权成功");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return data;
	 }
	
	/**
	 * 取消授权
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("guobo:grantCard:edit")
	@ResponseBody
	@RequestMapping(value = "cancelGrant")
	 public Map<String,Object> cancelGrant(HttpServletRequest request,HttpServletResponse response, Model model) {
		Map<String,Object> data = new HashMap<String,Object>();
		String id = request.getParameter("id");
		data.put("code", "1");
		data.put("msg", "取消授权失败");
		try{
			GrantCard grantCard = grantCardService.get(id);
			if(grantCard!=null){
				List<String> rfids = new ArrayList<String>();
				rfids.add(grantCard.getRfid());
				String result = OperationWebServiceUtils.grant("Cancel",rfids);
				if("0".equals(result)){
					grantCard.setStatus("0");
					grantCardService.save(grantCard);
					data.put("code", "0");
					data.put("msg", "取消授权成功");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return data;
	 }
}